<?php

class Article_Data_Mapper
{
    
private $userId;
private $articleCatId;  
private $articleName;
private $articleTags; 
private $addArticle;
private $articleFullText;
private $articlePrewievText;
private $time;
private $result;
private $dataMapperResult;
private $errors;
    
    /**
    public function __construct()
    {
        $service = Service::getInstance;
        и далее использовать сервис как insert, select , delete
    }
    
    */

    public function setAddInfo ($userId , $articleCat, $articleName, $articleTags , $addArticle)
    {
        $this->userId       = $userId;
        $this->articleCatId = $articleCat;       
        $this->articleName  = $articleName;       
        $this->articleTags  = $articleTags;       
        $this->addArticle   = $addArticle; 
        $this->time         = time();             
    }
     
    public function fetchTop10()
    {
        $sel = mysql_connect (DB_HOST , DB_LOGIN, DB_PASSWORD);
        mysql_select_db(DB_NAME, $sel);
        mysql_query('SET NAMES UTF8');
        
        $sql1 = "SELECT id , author_id, category_id, article_name, article_full_text, tags ,time_created 
                    FROM blogarticles
                    WHERE id>0 
                    ORDER BY id DESC
                    LIMIT 0,10
                    ";
        $query = mysql_query($sql1) or die('DB Query Error');
        
        $n = 0;
        while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
            $this->result[$n] = new Article_Model($row);        // храниться в резулт для возможности кэширования
            $n++;
        }
        mysql_close($sel);
        return $this->result;
    }
   
    
    public function fetchOne($id)
    {
        $sel = mysql_connect (DB_HOST , DB_LOGIN, DB_PASSWORD);
        mysql_select_db(DB_NAME, $sel);
        mysql_query('SET NAMES UTF8');
        
        $sql1 = "   SELECT id , author_id, category_id, article_name, article_full_text, tags ,time_created 
                    FROM blogarticles
                    WHERE id = $id 
                    ";
        $query = mysql_query($sql1) or die('DB Query Error');
        
        $n = 0;
        if ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
            $this->result[$n] = new Article_Model($row);         // храниться в резулт для возможности кэширования
        }
        mysql_close($sel);
        return $this->result;
    }

       
    public function save()
    {
        $sel = mysql_connect (DB_HOST , DB_LOGIN, DB_PASSWORD);
        mysql_select_db(DB_NAME, $sel);
        mysql_query('SET NAMES UTF8');   
        // положить всё в blogArticles
        $sql3 = "
                    INSERT INTO blogarticles (author_id , category_id, article_name, tags,article_full_text, time_created) 
                    VALUES( 
                                '$this->userId',
                                '$this->articleCatId',
                                '$this->articleName',
                                '$this->articleTags',
                                '$this->addArticle',
                                '$this->time'                              
                    )";
        
        if ($result3 = mysql_query($sql3)) {
            $this->result = true;
        } else {
            $this->result = false;
        }


        
         if ($this->result !== false){
             $this->dataMapperResult = true;
             mysql_close($sel);
         } else {
             $this->dataMapperResult = false;
             $this->error = 'не удалось сделать запрос';
             mysql_close($sel);
         }
         
    }
    
    public function getMapperResult() 
    {
        return $this->result;
    }
    
    public function getErrors() 
    {
        return $this->errors;
    }
    
    public function getResult() 
    {
        return $this->result;
    }
    
}
